<?php
/**
 * Created by PhpStorm.
 * User: yangxu
 * Date: 2016/5/3
 * Time: 16:36
 */

namespace Admin\Controller;
use Think\Controller;

class QuestionController extends Controller{
    private $question;
    private $cat;

    public function __construct(){
        parent::__construct();
        if(!logAdmin(session('adminId'))) header('location:'.U('Admin/Index/login'));
        $this->question=M('Questions');
        $this->cat=M('Category');
    }
    /*
     * 题库列表
     */
    public function index(){
        $page=isset($_GET['p'])?$_GET['p']:1;
        $catId=isset($_GET['catId'])?$_GET['catId']:null;
        if(!empty($catId)){
           $where['cat_id']=$catId;
        }else{
            $where='';
        }
        $count=$this->question->where($where)->count();
        vendor('Page');
        $pageClass=new \Page();
        $totalPage = ceil($count/10);
        $show=$pageClass->PageCut($page,$totalPage,$count,10);
        $this->assign('page',$show);
        $questionList=$this->question->where($where)->page($page,10)->select();
        $this->assign('questionList',$questionList);
        $this->assign('catId',$catId);
        $this->display();
    }

    /**
     * 导入题库域名
     */
    public function addQuestion(){
//        //查询分类
//        $list=$this->cat->select();
//        //引入树状类
//        vendor('Tree');
//        //实例化类
//        $tree=new \Tree();
//        //初始化数据
//        $tree->tree($list);
//        //取出树状结构
//        $catList=$tree->getRet();
//        $this->assign('catlist',$catList);
        $this->display('add');
    }

    /**
     * 导入题库操作
     */
    public function questionAdd(){
        if($_POST){
            header("Content-type: text/html; charset=utf-8");
            $xlsfile= $_SERVER['DOCUMENT_ROOT'].$_POST['fileUrl'];
            $excelnum=0;
            //读取excel文件
            Vendor('excel.reader');
            $excelData = new \Spreadsheet_Excel_Reader();
            $excelData->setOutputEncoding('UTF-8');
            $excelData->read($xlsfile);
            error_reporting(E_ALL ^ E_NOTICE);
            for ($i=2;$i<=$excelData->sheets[0]['numRows'];$i++){
                //定义excel字段数组
                $excelarr=array();
                for ($j=0; $j<=$excelData->sheets[0]['numCols'];$j++) {
                    //显示每个单元格内容
                    $excelarr[]=trim($excelData->sheets[0]['cells'][$i][$j]);
                }
                $excelarr=array_filter($excelarr);
                if(empty($excelarr)){
                    continue;
                }
                $options=array(
                    'A' => $excelarr['5'],
                    'B' => $excelarr['6'],
                    'C' => $excelarr['7'],
                    'D' => $excelarr['8'],
                    'E' => $excelarr['9'],
                );
                $options=array_filter($options);
                //插入数据库内容
                $data=array(
                    'cat_id'=>$_POST['catID'],//章节
                    'topic'=>$excelarr[2],//小节
                    'options'=>json_encode($options),//题干
                    'answer'=>$excelarr[3],//问题
                    'resolve'=>$excelarr[4],//分数
                    'type'=>$this->getType($excelarr[1]),//解析
                    'addtime'=>time()//添加时间
                );
                $this->question->add($data);
                $excelnum++;
//
            }
            @unlink($xlsfile);
            if($excelnum < 0) {
                $this->error('导入失败！');
            }else{
                $this->success('导入'.$excelnum.'道试题成功！');
            }
        }

    }

    /**
     * @param $str
     * @return int
     * @action getType
     * 作用：获取题目类型代码
     */
    private function getType($str){
        switch($str){
            case '单选题':
                $re=1;
                break;
            case '多选题':
                $re=2;
                break;
            case '判断题':
                $re=3;
                break;
        }
        return $re;
    }

    /**
     * 删除题目
     */
    public function delQuestion(){
        if($this->question->where('id='.$_GET['id'])->delete()) {
            $this->success('删除成功！');
        }else{
            $this->error('删除失败！');
        }
    }

    /**
     * 批量删除题目
     */
    public function allDel(){
        $id=$_GET['actionid'];
        $id=explode('-',$id);
        $where['id']=array('in',$id);
        if($this->question->where($where)->delete()){
            $this->success('删除成功！');
        }else{
            $this->error('删除失败！');
        }
    }
}